<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\UserLevelRelations;

class UserLevelRelationsRepository
{
    /**
     * 创建用户子商户
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return UserLevelRelations::create($params);
    }

    /**
     * 根据id获取还款账单资料
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return UserLevelRelations::find($id);
    }

    /**
     * 查找用户等级关系，返回一条数据
     * @param $userId
     * @return mixed
     */
    public function first($userId)
    {
        return UserLevelRelations::where([
            'user_id' => $userId,
        ])->first();
    }

    public function updateUserLevelId($userId, $levelId)
    {
        return UserLevelRelations::where([
            'user_id' => $userId,
        ])->update([
            'level_id' => $levelId,
            'update_time' => time(),
        ]);
    }

    /**
     * 根据pid获取所有下级统计
     * @param $pid
     * @return mixed
     */
    public function getUserLevelRelationsByPidCount($pid)
    {
        return UserLevelRelations::where('pid', $pid)->count();
    }

    public function getUserLevelRelationsByPidCountByCreateTime($pid, $startTime, $endTime)
    {
        return UserLevelRelations::where('pid', $pid)
            ->whereBetween('create_time', [$startTime, $endTime])
            ->count();
    }

    /**
     * 根据pid获取所有下级
     * @param $pid
     * @return mixed
     */
    public function getUserLevelRelationsByPid($pid)
    {
        return UserLevelRelations::where('pid', $pid)
            ->get()->toArray();
    }

    public function getUserLevelRelationsByPidByCreateTime($pid, $startTime, $endTime)
    {
        return UserLevelRelations::where('pid', $pid)
            ->whereBetween('create_time', [$startTime, $endTime])
            ->get()->toArray();
    }

    /**
     * 根据pid获取所有下级分页
     * @param $pid
     * @return mixed
     */
    public function getUserLevelRelationsByPidPaginate($pid, $page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        $offset = $page * $limit;
        return UserLevelRelations::where('pid', $pid)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('level_id', 'asc')
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
    }

    /**
     * 根据pid和等级id获取所有下级
     * @param $pid
     * @return mixed
     */
    public function getUserLevelRelationsByPidAndLevel($pid, $levelId)
    {
        return UserLevelRelations::where([
            'pid' => $pid,
            'level_id' => $levelId,
        ])
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
    }

    /**
     * 根据pid和等级id获取所有下级
     * @param $pid
     * @return mixed
     */
    public function getUserLevelRelationsByPidAndLevelPaginate($pid, $levelId, $page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        $offset = $page * $limit;
        return UserLevelRelations::where([
            'pid' => $pid,
        ])
            ->whereIn('level_id', $levelId)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('level_id', 'asc')
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
    }
}